﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Ylh.FactoryDAL;
using Ylh.IDAL;

namespace Ylh.BLL
{
    public class CustBLL
    {
        private string ConStr = string.Empty;

        public CustBLL(string ConStr)
        {
            this.ConStr = ConStr;
        }

        /// <summary>
        /// 插入一条客户信息,并且返回客户编号
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustName">客户名称</param>
        /// <param name="BusinessStaffID">业务员编号</param>
        /// <param name="IsProtect">是否是保护客户</param>
        /// <returns></returns>
        public int InsertInto(string CustName, string BusinessStaffID, string IsProtect, string CompanyName, string TelPhone, string CustAddress, string Legal)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.InsertInto(ConStr, CustName, BusinessStaffID, IsProtect, CompanyName, TelPhone, CustAddress, Legal);
        }



        public DataTable GetCustByCodition( string condition)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.GetCustByCodition(ConStr,condition);
        }


        /// <summary>
        /// 获取否个员工下面的所有客户
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="BusinessStaffID">业务员编号</param>
        /// <param name="isProtect"></param>
        /// <returns></returns>
       public  DataTable GetCustList(string BusinessStaffID, bool? isProtect)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.GetCustList(ConStr, BusinessStaffID, isProtect);
        }

        /// <summary>
        /// 获取否个员工下面的所有客户
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="BusinessStaffID"></param>
        /// <param name="isProtect"></param>
        /// <param name="?"></param>
        /// <returns></returns>
       public DataTable GetCustList(string BusinessStaffID, bool? isProtect, int PageCurrent, int PageSize, ref int PageCount)
       {
           Ylh.IDAL.ICust icu = DataAccess.CreateICust();
           return icu.GetCustList(ConStr, BusinessStaffID, isProtect, PageCurrent, PageSize, ref PageCount);
       }

       /// <summary>
       /// 获取某个客服人员下面的客户
       /// </summary>
       /// <param name="ConStr"></param>
       /// <param name="ServerStaffID">客服人员编号,如果为-1则获取未分配客服的客户</param>
       /// <returns></returns>
       public DataTable GetCustList(int ServerStaffID)
       {
           Ylh.IDAL.ICust icu = DataAccess.CreateICust();

           return icu.GetCustList(ConStr,ServerStaffID);
       }

        /// <summary>
        /// 更新客户名称
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustName"></param>
        /// <param name="CustID"></param>
        /// <returns></returns>
        public bool UpdateCustName(string CustName, string CustID)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.UpdateCustName(ConStr, CustName, CustID);
        }

        /// <summary>
        ///  更新客户的移交时间
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustID"></param>
        /// <returns></returns>
        public bool UpdateTransferInfo(string CustID)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();
            return icu.UpdateTransferInfo(ConStr, CustID);
        }



        /// <summary>
        /// 更新客户的保护状态
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustID">客户编号</param>
        /// <param name="IsProtect">是否是保护</param>
        /// <returns></returns>
        public bool UpdateProtectInfo(string CustID, bool IsProtect)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.UpdateProtectInfo(ConStr, CustID, IsProtect);
        }


        /// <summary>
        /// 更新客服人员
        /// </summary>
        /// <param name="CustID"></param>
        /// <param name="ServerStaffID">客服人员编号</param>
        /// <returns></returns>
        public bool UpdateServerInfo(string CustID, string ServerStaffID)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.UpdateServerInfo(ConStr, CustID, ServerStaffID);
        }



        /// <summary>
        /// 检查保护客户是否超过角色设定的最大值
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="staffID"></param>
        /// <returns></returns>
        public bool CheckProtect(string RoleID, string staffID)
        {
            if (RoleID == "-1")
            {
                return false;
            }

            Ylh.IDAL.ICust icu = DataAccess.CreateICust();
            Ylh.IDAL.IRole ir = DataAccess.CreateIRole();
            if (ir.GetProtectCount(ConStr, RoleID) - icu.CountProtect(ConStr, staffID) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Ylh.Model.TbCust GetModel(int CustID)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.GetModel(ConStr, CustID);
        }




        public string GetCustName(string CustID)
        {
          Ylh.Model.TbCust cust =GetModel( Convert.ToInt32( CustID));

          return cust.CustName + "[" + cust.CompanyName + "]";

        }




        public bool UpdateCustInfoBuss( Ylh.Model.TbCust model)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();

            return icu.UpdateCustInfoBuss(ConStr, model);
        }

        /// <summary>
        /// 插入客户信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="model">客户信息对象</param>
        /// <returns></returns>
        public int InsertCustInfo(Ylh.Model.TbCust model)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();
            return icu.InsertCustInfo(ConStr,model);
        }
        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="model">客户对象</param>
        /// <returns></returns>
        public bool UpdateCustInfo(Ylh.Model.TbCust model)
        {
            Ylh.IDAL.ICust icu = DataAccess.CreateICust();
            return icu.UpdateCustInfo( ConStr, model);
        }



        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustID">客户编号</param>
        /// <returns></returns>
        public bool DeleteCust(string CustID)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.DeleteCust(ConStr, CustID);
        }




        public void deleteCust(List<string> li)
        {
            foreach (string  str in li)
            {
                DeleteCust(str);
            }
        }



        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="sql">数据库语句</param>
        /// <param name="model">客户对象</param>
        /// <returns></returns>
        public bool UpdateCustServerInfo( string sql, Ylh.Model.TbCust model)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.UpdateCustServerInfo(ConStr, sql, model);
        }



        public DataTable GetCustListByCondition(string StaffID, bool? isProtect, bool IsDelet)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.GetCustListByCondition(ConStr, StaffID, isProtect, IsDelet);
        }

        /// <summary>
        /// 重载,获取指定员工的已经转移至客服经理的客户
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="BusinessStaffID">员工编号</param>
        /// <param name="IsTransfer">是否已经转移</param>
        /// <returns></returns>
        public DataTable GetCustList(string BusinessStaffID, bool IsTransfer, string sTransfer)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.GetCustList(ConStr, BusinessStaffID, IsTransfer, sTransfer);
        }

        /// <summary>
        /// 获取已经有的意见
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="CustID">客户编号</param>
        /// <returns>当前意见,如果没有则返回空</returns>
        public string GetOpinion(string CustID)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.GetOpinion(ConStr, CustID);
        }
        /// <summary>
        /// 更新客户的意见
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="Opinion">意见</param>
        /// <param name="CustID"></param>
        /// <returns>成功为True,不成功为false</returns>
        public bool UpdateOpinion(string Opinion, string CustID)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.UpdateOpinion(ConStr, Opinion, CustID);
        }

        /// <summary>
        /// 重载,获取员工信息
        /// </summary>
        /// <param name="Opinion"></param>
        /// <param name="CustID"></param>
        /// <param name="StaffID"></param>
        public void UpdateOpinion(string Opinion, string CustID, decimal StaffID)
        {

            Ylh.BLL.StaffBLL bll = new StaffBLL(ConStr);

             DataTable dt=bll.GetStaff(Convert.ToInt32(StaffID));

             string NewOpinion = dt.Rows[0]["StaffName"].ToString() + "[" + dt.Rows[0]["RoleName"].ToString() + "]" + "　　　　　　" + DateTime.Now.ToString() + "</br>" + Opinion + "</br>-------------------------------------------------------------------------</br>";

            NewOpinion = NewOpinion + GetOpinion(CustID);

             UpdateOpinion(NewOpinion, CustID);
  


        }




        /// <summary>
        /// 修改客户业务员
        /// </summary>
        /// <param name="CustID">客户编号</param>
        /// <param name="StaffID">业务员编号</param>
        /// <returns></returns>
        public bool UpdateBussinStaff(string CustID, string StaffID)
        {
            Ylh.IDAL.ICust ic = DataAccess.CreateICust();
            return ic.UpdateBussinStaff(ConStr, CustID, StaffID);
        }


    }
}
